/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.reports;

import com.railway.reports.utils.QueryBuilder;
import com.railway.utils.CustomScrollBarUI;
import com.railway.utils.RestServer;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Kenny
 */
public class TicketCustomizer extends javax.swing.JPanel {

    private DefaultComboBoxModel dcbmFromStations, dcbmToStations;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private boolean range = false;
    private boolean status = false;
    private boolean oriNDes = false;
    private int initialHave = 0;
    private QueryBuilder qb;

    /**
     * Creates new form TicketCustomizer
     */
    public TicketCustomizer() {
        initComponents();
        populateStations();
    }

    private void populateStations() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                dcbmFromStations = new DefaultComboBoxModel();
                dcbmToStations = new DefaultComboBoxModel();
                String result = RestServer.httpGet("station", "");
                try {
                    JSONObject json = new JSONObject(result);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int statuscode = json.getInt("status");
                        if (statuscode == 200) {
                            JSONArray jsonArr = json.getJSONArray("content");
                            for (int i = 0; i < jsonArr.length(); i++) {
                                dcbmFromStations.addElement(jsonArr.getJSONObject(i).getString("stationName"));
                                dcbmToStations.addElement(jsonArr.getJSONObject(i).getString("stationName"));
                            }
                            jcbxOrigin.setModel(dcbmFromStations);
                            jcbxDestin.setModel(dcbmToStations);
                        }
                    }
                } catch (JSONException ex) {
                }
            }
        }).start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbgDateRange = new javax.swing.ButtonGroup();
        jbgLocation = new javax.swing.ButtonGroup();
        jlblTicketCustomizer = new javax.swing.JLabel();
        jsprCustomizer = new javax.swing.JSeparator();
        jycYear = new com.toedter.calendar.JYearChooser();
        jdcFrom = new com.toedter.calendar.JDateChooser();
        jlblAnd = new javax.swing.JLabel();
        jdcTo = new com.toedter.calendar.JDateChooser();
        jrbRangeYear = new javax.swing.JRadioButton();
        jrbRangeMonth = new javax.swing.JRadioButton();
        jrbRangeBetween = new javax.swing.JRadioButton();
        jspiMonth = new javax.swing.JSpinner();
        jcbRangeOfDate = new javax.swing.JCheckBox();
        jcbTicketStatus = new javax.swing.JCheckBox();
        jspStatus = new javax.swing.JScrollPane();
        jltStatus = new javax.swing.JList();
        jcbTicketOriDes = new javax.swing.JCheckBox();
        jrbOrigin = new javax.swing.JRadioButton();
        jrbDestin = new javax.swing.JRadioButton();
        jcbxOrigin = new javax.swing.JComboBox();
        jcbxDestin = new javax.swing.JComboBox();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        setPreferredSize(new java.awt.Dimension(210, 340));
        setLayout(null);

        jlblTicketCustomizer.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblTicketCustomizer.setText("CRITERIA CUSTOMIZER");
        add(jlblTicketCustomizer);
        jlblTicketCustomizer.setBounds(10, 5, 200, 20);
        add(jsprCustomizer);
        jsprCustomizer.setBounds(5, 25, 200, 5);

        jycYear.setEnabled(false);
        add(jycYear);
        jycYear.setBounds(100, 50, 100, 20);

        jdcFrom.setEnabled(false);
        jdcFrom.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        add(jdcFrom);
        jdcFrom.setBounds(100, 90, 100, 20);

        jlblAnd.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlblAnd.setText("and");
        jlblAnd.setEnabled(false);
        add(jlblAnd);
        jlblAnd.setBounds(40, 110, 60, 20);

        jdcTo.setEnabled(false);
        jdcTo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        add(jdcTo);
        jdcTo.setBounds(100, 110, 100, 20);

        jrbRangeYear.setBackground(new java.awt.Color(255, 255, 255));
        jbgDateRange.add(jrbRangeYear);
        jrbRangeYear.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbRangeYear.setSelected(true);
        jrbRangeYear.setText("Year");
        jrbRangeYear.setActionCommand("year");
        jrbRangeYear.setEnabled(false);
        jrbRangeYear.setFocusPainted(false);
        jrbRangeYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbRangeYearActionPerformed(evt);
            }
        });
        add(jrbRangeYear);
        jrbRangeYear.setBounds(20, 50, 80, 20);

        jrbRangeMonth.setBackground(new java.awt.Color(255, 255, 255));
        jbgDateRange.add(jrbRangeMonth);
        jrbRangeMonth.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbRangeMonth.setText("Month");
        jrbRangeMonth.setActionCommand("month");
        jrbRangeMonth.setEnabled(false);
        jrbRangeMonth.setFocusPainted(false);
        jrbRangeMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbRangeMonthActionPerformed(evt);
            }
        });
        add(jrbRangeMonth);
        jrbRangeMonth.setBounds(20, 70, 80, 20);

        jrbRangeBetween.setBackground(new java.awt.Color(255, 255, 255));
        jbgDateRange.add(jrbRangeBetween);
        jrbRangeBetween.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbRangeBetween.setText("Between");
        jrbRangeBetween.setActionCommand("between");
        jrbRangeBetween.setEnabled(false);
        jrbRangeBetween.setFocusPainted(false);
        jrbRangeBetween.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbRangeBetweenActionPerformed(evt);
            }
        });
        add(jrbRangeBetween);
        jrbRangeBetween.setBounds(20, 90, 80, 20);

        jspiMonth.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jspiMonth.setModel(new javax.swing.SpinnerNumberModel(1, 1, 12, 1));
        jspiMonth.setEnabled(false);
        add(jspiMonth);
        jspiMonth.setBounds(100, 70, 100, 20);

        jcbRangeOfDate.setBackground(new java.awt.Color(255, 255, 255));
        jcbRangeOfDate.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jcbRangeOfDate.setText("Range of Date");
        jcbRangeOfDate.setActionCommand("datePurchased");
        jcbRangeOfDate.setFocusPainted(false);
        jcbRangeOfDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbRangeOfDateActionPerformed(evt);
            }
        });
        add(jcbRangeOfDate);
        jcbRangeOfDate.setBounds(10, 30, 190, 20);

        jcbTicketStatus.setBackground(new java.awt.Color(255, 255, 255));
        jcbTicketStatus.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jcbTicketStatus.setText("Status of Ticket");
        jcbTicketStatus.setFocusPainted(false);
        jcbTicketStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbTicketStatusActionPerformed(evt);
            }
        });
        add(jcbTicketStatus);
        jcbTicketStatus.setBounds(10, 130, 190, 20);

        jltStatus.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jltStatus.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "PAD", "AMD", "CCL", "CHK" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jltStatus.setEnabled(false);
        jspStatus.setViewportView(jltStatus);

        jspStatus.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        add(jspStatus);
        jspStatus.setBounds(30, 155, 170, 70);

        jcbTicketOriDes.setBackground(new java.awt.Color(255, 255, 255));
        jcbTicketOriDes.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jcbTicketOriDes.setText("Ticket's Location");
        jcbTicketOriDes.setFocusPainted(false);
        jcbTicketOriDes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbTicketOriDesActionPerformed(evt);
            }
        });
        add(jcbTicketOriDes);
        jcbTicketOriDes.setBounds(10, 230, 190, 20);

        jrbOrigin.setBackground(new java.awt.Color(255, 255, 255));
        jbgLocation.add(jrbOrigin);
        jrbOrigin.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbOrigin.setSelected(true);
        jrbOrigin.setText("Origin");
        jrbOrigin.setActionCommand("ori");
        jrbOrigin.setEnabled(false);
        jrbOrigin.setFocusPainted(false);
        jrbOrigin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbOriginActionPerformed(evt);
            }
        });
        add(jrbOrigin);
        jrbOrigin.setBounds(20, 250, 90, 20);

        jrbDestin.setBackground(new java.awt.Color(255, 255, 255));
        jbgLocation.add(jrbDestin);
        jrbDestin.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jrbDestin.setText("Destination");
        jrbDestin.setActionCommand("des");
        jrbDestin.setEnabled(false);
        jrbDestin.setFocusPainted(false);
        jrbDestin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbDestinActionPerformed(evt);
            }
        });
        add(jrbDestin);
        jrbDestin.setBounds(20, 290, 90, 20);

        jcbxOrigin.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jcbxOrigin.setEnabled(false);
        add(jcbxOrigin);
        jcbxOrigin.setBounds(40, 270, 160, 20);

        jcbxDestin.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jcbxDestin.setEnabled(false);
        add(jcbxDestin);
        jcbxDestin.setBounds(40, 310, 160, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void jrbRangeYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbRangeYearActionPerformed
        jycYear.setEnabled(true);
        jspiMonth.setEnabled(false);
        jdcFrom.setEnabled(false);
        jdcTo.setEnabled(false);
    }//GEN-LAST:event_jrbRangeYearActionPerformed

    private void jrbRangeMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbRangeMonthActionPerformed
        jycYear.setEnabled(false);
        jspiMonth.setEnabled(true);
        jdcFrom.setEnabled(false);
        jdcTo.setEnabled(false);
    }//GEN-LAST:event_jrbRangeMonthActionPerformed

    private void jrbRangeBetweenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbRangeBetweenActionPerformed
        jycYear.setEnabled(false);
        jspiMonth.setEnabled(false);
        jdcFrom.setEnabled(true);
        jdcTo.setEnabled(true);
    }//GEN-LAST:event_jrbRangeBetweenActionPerformed

    private void jcbRangeOfDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbRangeOfDateActionPerformed
        initialHave = 0;
        if (jcbRangeOfDate.isSelected()) {
            range = true;
            jrbRangeYear.setEnabled(true);
            jrbRangeMonth.setEnabled(true);
            jrbRangeBetween.setEnabled(true);
            jlblAnd.setEnabled(true);
            jycYear.setEnabled(true);
        } else {
            range = false;
            jrbRangeYear.setEnabled(false);
            jrbRangeMonth.setEnabled(false);
            jrbRangeBetween.setEnabled(false);
            jlblAnd.setEnabled(false);
            jycYear.setEnabled(false);
            jspiMonth.setEnabled(false);
            jdcFrom.setEnabled(false);
            jdcTo.setEnabled(false);
        }
    }//GEN-LAST:event_jcbRangeOfDateActionPerformed

    private void jcbTicketOriDesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbTicketOriDesActionPerformed
        initialHave = 0;
        if (jcbTicketOriDes.isSelected()) {
            oriNDes = true;
            jrbOrigin.setEnabled(true);
            jrbDestin.setEnabled(true);
            jcbxOrigin.setEnabled(true);
        } else {
            oriNDes = false;
            jrbOrigin.setEnabled(false);
            jrbDestin.setEnabled(false);
            jcbxOrigin.setEnabled(false);
            jcbxDestin.setEnabled(false);
        }
    }//GEN-LAST:event_jcbTicketOriDesActionPerformed

    private void jcbTicketStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbTicketStatusActionPerformed
        initialHave = 0;
        if (jcbTicketStatus.isSelected()) {
            status = true;
            jltStatus.setEnabled(true);
            jltStatus.setSelectedIndex(0);
        } else {
            status = false;
            jltStatus.setEnabled(false);
        }
    }//GEN-LAST:event_jcbTicketStatusActionPerformed

    private void jrbOriginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbOriginActionPerformed
        jcbxOrigin.setEnabled(true);
        jcbxDestin.setEnabled(false);
    }//GEN-LAST:event_jrbOriginActionPerformed

    private void jrbDestinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbDestinActionPerformed
        jcbxOrigin.setEnabled(false);
        jcbxDestin.setEnabled(true);
    }//GEN-LAST:event_jrbDestinActionPerformed

    private void rangeHaving() {
        String selectedRange = jbgDateRange.getSelection().getActionCommand();
        switch (selectedRange) {
            case "year":
                qb.having("YEAR(datePurchased)", "" + jycYear.getValue());
                break;
            case "month":
                qb.having("MONTH(datePurchased)", "" + jspiMonth.getValue());
                break;
            case "between":
                qb.havingBetween("datePurchased", format.format(jdcFrom.getDate()), format.format(jdcTo.getDate()));
                break;
        }
    }

    private void rangeAnd() {
        String selectedRange = jbgDateRange.getSelection().getActionCommand();
        switch (selectedRange) {
            case "year":
                qb.and("YEAR(datePurchased)", "" + jycYear.getValue());
                break;
            case "month":
                qb.and("MONTH(datePurchased)", "" + jspiMonth.getValue());
                break;
            case "between":
                qb.andBetween("datePurchased", format.format(jdcFrom.getDate()), format.format(jdcTo.getDate()));
                break;
        }
    }

    private void statusHaving() {
        List<String> statusList = jltStatus.getSelectedValuesList();
        qb.having("status", statusList.get(0));
        for (int i = 1; i < statusList.size(); i++) {
            qb.or("status", statusList.get(i));
        }
    }

    private void statusAnd() {
        List<String> statusList = jltStatus.getSelectedValuesList();
        if (!statusList.isEmpty()) {
            qb.and("status", statusList.get(0));
            for (int i = 1; i < statusList.size(); i++) {
                qb.or("status", statusList.get(i));
            }
        }
    }

    private void locationHaving() {
        String selectedloc = jbgLocation.getSelection().getActionCommand();
        switch (selectedloc) {
            case "ori":
                qb.having("origin", jcbxOrigin.getSelectedItem().toString());
                break;
            case "des":
                qb.having("destination", jcbxDestin.getSelectedItem().toString());
                break;
        }
    }

    private void locationAnd() {
        String selectedloc = jbgLocation.getSelection().getActionCommand();
        switch (selectedloc) {
            case "ori":
                qb.and("origin", jcbxOrigin.getSelectedItem().toString());
                break;
            case "des":
                qb.and("destination", jcbxDestin.getSelectedItem().toString());
                break;
        }
    }

    public String buildQuery() {
        qb = new QueryBuilder();
        qb.select("referenceNo", "departureDate", "datePurchased", "origin", "destination", "class", "fare", "status").from("ticket");
        if (range) {
            System.out.println(initialHave);
            if (initialHave != 2 && initialHave != 3) {
                initialHave = 1;
                rangeHaving();
            } else {
                rangeAnd();
            }
        }
        if (status) {
            System.out.println(initialHave);
            if (initialHave != 1 && initialHave != 3) {
                initialHave = 2;
                statusHaving();
            } else {
                statusAnd();
            }

        }
        if (oriNDes) {
            System.out.println(initialHave);
            if (initialHave != 1 && initialHave != 2) {
                initialHave = 3;
                locationHaving();
            } else {
                locationAnd();
            }
        }
        return qb.build();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup jbgDateRange;
    private javax.swing.ButtonGroup jbgLocation;
    private javax.swing.JCheckBox jcbRangeOfDate;
    private javax.swing.JCheckBox jcbTicketOriDes;
    private javax.swing.JCheckBox jcbTicketStatus;
    private javax.swing.JComboBox jcbxDestin;
    private javax.swing.JComboBox jcbxOrigin;
    private com.toedter.calendar.JDateChooser jdcFrom;
    private com.toedter.calendar.JDateChooser jdcTo;
    private javax.swing.JLabel jlblAnd;
    private javax.swing.JLabel jlblTicketCustomizer;
    private javax.swing.JList jltStatus;
    private javax.swing.JRadioButton jrbDestin;
    private javax.swing.JRadioButton jrbOrigin;
    private javax.swing.JRadioButton jrbRangeBetween;
    private javax.swing.JRadioButton jrbRangeMonth;
    private javax.swing.JRadioButton jrbRangeYear;
    private javax.swing.JScrollPane jspStatus;
    private javax.swing.JSpinner jspiMonth;
    private javax.swing.JSeparator jsprCustomizer;
    private com.toedter.calendar.JYearChooser jycYear;
    // End of variables declaration//GEN-END:variables
}
